package com.mischa.doedeljas.client.ui;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Vector;
import javax.swing.BorderFactory;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ScrollPaneLayout;
import javax.swing.SwingUtilities;
import javax.swing.border.BevelBorder;

import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import com.mischa.doedeljas.Card;
import com.mischa.doedeljas.Card.Suit;
import com.mischa.doedeljas.client.DoedelJas;
import com.mischa.doedeljas.client.GameClient;
import com.mischa.doedeljas.client.ui.CardsView.SelectionChangedListener;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class DealerDialog 
	extends javax.swing.JDialog implements SelectionChangedListener, ActionListener
{
	/**
	 * 
	 */
	private static final long serialVersionUID = -8598979461025683425L;

	private GameClient _game;
	private Vector<Card> _hand;
	private Vector<Card> _dumped = new Vector<Card>();
	private JButton _acceptButton;
	private Suit _trumpSuit = null;
	
	private CardsView _trumpSuits;
	private CardsView _cardsToDump;
	
	private CardsView _trumpSuitPreview;
	private CardsView _dumpedCardsPreview;
	
	private JLabel _dumpedCards;
	private JLabel _trumpChosen;	
	private JLabel _label;

	public DealerDialog(JFrame frame) {
		super(frame);
		initGUI();
	}
	
	public void setGame(GameClient game)
	{
		this._game = game;
	}
	
	public void setHand(Vector<Card> hand)
	{
		this._hand = new Vector<Card>();
		this._hand.addAll(hand);
		_cardsToDump.add(this._hand);
	}
	
	private void initGUI() {
		try {
			getContentPane().setLayout(null);
			this.setTitle("You are the dealer");
			{
				_label = new JLabel();
				getContentPane().add(_label);
				_label.setText("You are the dealer. Please select a trump suit and dump two cards from your hand.");
				_label.setBounds(0, 0, 524, 15);
				_label.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
			}
			{
				_trumpSuits = new CardsView();
				for (int i = 1; i <= 4; ++i)
				{
					_trumpSuits.add(new Card(10 * i + 1));
				}
				getContentPane().add(_trumpSuits);
				_trumpSuits.setBounds(12, 27, 512, 74);
				_trumpSuits.setSelectionChangedListener(this);
				_trumpSuits.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
			}
			{	
				_cardsToDump = new CardsView();						
				getContentPane().add(_cardsToDump);
				_cardsToDump.setBounds(12, 107, 1057, 93);
				_cardsToDump.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));				
				_cardsToDump.setSelectionChangedListener(this);
				_cardsToDump.setPreferredSize(new java.awt.Dimension(1003, 93));
				_cardsToDump.setSize(1057, 93);
			}
			{
				_trumpChosen = new JLabel();
				getContentPane().add(_trumpChosen);
				_trumpChosen.setText("Chosen trump suit:");
				_trumpChosen.setBounds(12, 225, 119, 15);
				_trumpChosen.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
			}
			{
				_dumpedCards = new JLabel();
				getContentPane().add(_dumpedCards);
				_dumpedCards.setText("Dumped cards:");
				_dumpedCards.setBounds(171, 225, 95, 15);				
				_dumpedCards.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
			}
			{
				_trumpSuitPreview = new CardsView();
				getContentPane().add(_trumpSuitPreview);
				_trumpSuitPreview.setBounds(24, 246, 93, 78);
				_trumpSuitPreview.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
			}
			{
				_dumpedCardsPreview = new CardsView();
				getContentPane().add(_dumpedCardsPreview);
				_dumpedCardsPreview.setBounds(171, 246, 165, 78);
				_dumpedCardsPreview.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
			}
			{
				_acceptButton = new JButton();
				getContentPane().add(_acceptButton);
				_acceptButton.setText("OK");
				_acceptButton.setBounds(367, 302, 157, 22);
				_acceptButton.addActionListener(this);
			}
			this.setSize(529, 330);
			this.setModal(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void selectionChanged(CardsView cardsView, Card selectedCard) {
		if (cardsView == _trumpSuits)
		{
			_trumpSuit = selectedCard.getSuit();
			_trumpSuitPreview.clear();
			_trumpSuitPreview.add(selectedCard);
		}
		else if (cardsView == _cardsToDump)
		{
			if (_hand.contains(selectedCard))
			{
				if (!_dumped.contains(selectedCard))
				{
					_dumped.add(selectedCard);
				}
				
				while (_dumped.size() > 2)
				{
					_dumped.remove(0);
				}
				
				_dumpedCardsPreview.clear();
				_dumpedCardsPreview.add(_dumped);
			}
		}
		
		validate();
	}

	@Override
	public void actionPerformed(ActionEvent e) 
	{
		if (e.getSource() == _acceptButton)
		{
			if (_dumped.size() != 2)
			{
				JOptionPane.showMessageDialog(this, "You must select two cards to dump.", "Error", JOptionPane.ERROR_MESSAGE);
				return;
			}
			
			if (_trumpSuit == null)
			{
				JOptionPane.showMessageDialog(this, "You must select a trump suit.", "Error", JOptionPane.ERROR_MESSAGE);
				return;
			}
			
			_game.dumpAndTrump(_trumpSuit, _dumped.get(0), _dumped.get(1));			
			this.setVisible(false);
		}
	}
	
}
